﻿//===========================================================================================================================
// Cuberry - http://www.cuberry.net
// Copyright (c) 2012
// by Peacequare® - Amir Moussa
//===========================================================================================================================
// This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License 
// as published by the Free Software Foundation, either version 3 of the License, or any later version.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// This program is distributed in the hope that it will be useful.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//
// See the GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//===========================================================================================================================

using System;
using System.Collections.Generic;
using System.ComponentModel;
using Microsoft.Practices.Prism.Events;
using Microsoft.Practices.Prism.Commands;
using P2.Cuberry.Framework.Helper;


namespace P2.Cuberry.Modules.SimpleAccounting.FiAccount
{
    public class FiAccountsFindPresenter : INotifyPropertyChanged, IFiAccountsFindPresenter
    {
        public FiAccountsFindPresenter(IFiAccountsFindView view, Services.IFiAccountService service, IEventAggregator eventAggregator)
        {
            try
            {
                View = view;
                this._eventAggregator = eventAggregator;
                this.Model = new BusinessEntities.FiAccount();
                this.Service = service;
                this.AccountTypes = BusinessEntities.FiAccountType.GetList;
                this.FiscalYears = BusinessEntities.FiFiscalYear.GetList;
                WorkflowStatusses = P2.Cuberry.Framework.Helper.Workflows.WorkflowStatus.GetStatusListForObject(Model.EntityName);

                this.FiAccountAddCommand = new DelegateCommand<object>(OnFiAccountAddExecute, OnFiAccountAddCanExecute);
                this.FiAccountFindCommand = new DelegateCommand<object>(OnFiAccountFindExecute, OnFiAccountFindCanExecute);
                this.FiAccountClrCommand = new DelegateCommand<object>(OnFiAccountClrExecute, OnFiAccountClrCanExecute);

                if (this.FiscalYears.Count > 0)
                    Model.FiAccountFiscalYearID = this.FiscalYears[0].FiscalYearID;
                else
                    Model.FiAccountFiscalYearID = -1;
                Model.Chargeable = null;
                ControlRights = Model.GetControls(Properties.Settings.Default.ModuleName, "FiAccountsFindView");

                View.Presenter = this;
            }
            catch (Exception ex) //AM:TC
            {
                P2ExceptionHandler.LogException(ex, string.Format("{0}.{1}", this.GetType().Name, System.Reflection.MethodInfo.GetCurrentMethod().Name));
            }
        }


        #region "Members"

        public string FiAccountNo
        {
            get
            {
                return this.Model.FiAccountNo;
            }
            set
            {
                if (this.Model.FiAccountNo != value)
                {
                    this.Model.FiAccountNo = value;
                    OnPropertyChanged("FiAccountNo");
                }
            }
        }
        public string FiAccountNameCur
        {
            get { return this.Model.FiAccountNameCur; }
            set
            {
                if (this.Model.FiAccountNameCur != value)
                {
                    this.Model.FiAccountNameCur = value;
                    OnPropertyChanged("FiAccountNameCur");
                }
            }
        }

        public int FiAccountFiscalYearID
        {
            get { return Model.FiAccountFiscalYearID; }
            set
            {
                if (Model.FiAccountFiscalYearID != value)
                {
                    Model.FiAccountFiscalYearID = value;
                    OnPropertyChanged("FiAccountFiscalYearID");
                }
            }
        }

        public int FiAccountTypeID
        {
            get { return Model.FiAccountTypeID; }
            set
            {
                if (Model.FiAccountTypeID != value)
                {
                    Model.FiAccountTypeID = value;
                    OnPropertyChanged("FiAccountTypeID");
                }
            }
        }
        public bool? Chargeable
        {
            get { return this.Model.Chargeable; }
            set
            {
                if (this.Model.Chargeable != value)
                {
                    this.Model.Chargeable = value; OnPropertyChanged("Chargeable");
                }
            }
        }



        public int StatusID
        {
            get { return Model.StatusID; }
            set
            {
                if (Model.StatusID != value)
                {
                    Model.StatusID = value;
                    OnPropertyChanged("StatusID");
                }
            }
        }

        public byte? FiAccountLevel
        {
            get { return Model.FiAccountLevel; }
            set
            {
                if (Model.FiAccountLevel != value)
                {
                    Model.FiAccountLevel = value;
                    OnPropertyChanged("FiAccountLevel");
                }
            }
        }
        #endregion

        
        #region "Reference Tables"

        public List<BusinessEntities.FiAccountType> AccountTypes {get; set;}
        public List<BusinessEntities.FiFiscalYear> FiscalYears { get; set; }
        public List<P2.Cuberry.Framework.Helper.Workflows.WorkflowStatus> WorkflowStatusses { get; set; }
       
        #endregion


        #region Commands

        public DelegateCommand<object> FiAccountAddCommand { get; set; }
        public DelegateCommand<object> FiAccountClrCommand { get; set; }
        public DelegateCommand<object> FiAccountFindCommand { get; set; }


        void OnFiAccountAddExecute(object sourceAccount)
        {
            Events.AddFiAccountEvent addFiAccountEvent = this._eventAggregator.GetEvent<Events.AddFiAccountEvent>();
            addFiAccountEvent.Publish(this.Model);           
        }


        bool OnFiAccountAddCanExecute(object sourceAccount)
        {
            P2.Cuberry.Framework.Security.User.ControlRight controlRight;
            if (ControlRights.TryGetValue("FiAccountAdd", out controlRight))
                return controlRight.IsActive;
            else
                return false;
        }


        void OnFiAccountFindExecute(object sourceAccount)
        {
            try
            {
                this.View.UpdateFocusedControl();
                Events.FindFiAccountEvent findFiAccountEvent = this._eventAggregator.GetEvent<Events.FindFiAccountEvent>();
                findFiAccountEvent.Publish(this.Model);
            }
            catch (Exception ex) //AM:TC
            {
                P2ExceptionHandler.LogException(ex, string.Format("{0}.{1}", this.GetType().Name, System.Reflection.MethodInfo.GetCurrentMethod().Name));
            }
        }


        bool OnFiAccountFindCanExecute(object sourceAccount)
        {
            return true;
        }


        void OnFiAccountClrExecute(object sourceAccount)
        {
            try
            {
                this.View.UpdateFocusedControl();
                this.FiAccountNo = "";
                this.FiAccountNameCur = "";

                if (FiscalYears.Count > 0)
                    this.FiAccountFiscalYearID = FiscalYears[0].FiscalYearID;
                else
                    this.FiAccountFiscalYearID = -1;

                this.FiAccountTypeID = -1;
                this.Chargeable = null;
                this.FiAccountLevel = null;
                this.StatusID = -1;
            }
            catch (Exception ex) //AM:TC
            {
                P2ExceptionHandler.LogException(ex, string.Format("{0}.{1}", this.GetType().Name, System.Reflection.MethodInfo.GetCurrentMethod().Name));
            }
        }


        bool OnFiAccountClrCanExecute(object sourceAccount)
        {
            return true;
        }

        #endregion


        private IEventAggregator _eventAggregator;

        public IFiAccountsFindView View { get; set; }
        public Services.IFiAccountService Service;
        public BusinessEntities.FiAccount Model { get; set; }

        public Dictionary<string, P2.Cuberry.Framework.Security.User.ControlRight> ControlRights;

        public P2MessageStack MessageStack = new P2MessageStack();


        #region Property Changed

        public event PropertyChangedEventHandler PropertyChanged;


        private void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler Handler = PropertyChanged;
            if (Handler != null) Handler(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion


    }
}